// 初始化手风琴组件
document.addEventListener('DOMContentLoaded', function() {
    console.log('初始化手风琴组件');

    const isFolded = (ele) => ele.classList.contains('accordion-folded');
    const fold = (accordionItem) => accordionItem.classList.add('accordion-folded');
    const unfold = (accordionItem) => accordionItem.classList.remove('accordion-folded');
    const toggle = function(accordionItem) {
        if (isFolded(accordionItem))
            unfold(accordionItem);
        else
            fold(accordionItem);
    };

    let accordionArr = Array.from(document.querySelectorAll('.accordion'));
    accordionArr.forEach(function(accordion) {
        // 获取所有 AccordionItem
        let accordionItemArr = Array.from(accordion.querySelectorAll('.accordion-item'));
        accordionItemArr.forEach(function(accordionItem) {
            // 绑定事件
            let title = accordionItem.querySelector('.accordion-title');
            title.addEventListener('click', function() {
                // 先处理自己
                toggle(accordionItem);

                // 再处理其他人
                accordionItemArr.forEach(function(item) {
                    if (item !== accordionItem)
                        fold(item);
                });
            });
        });
    });
});